//
//  YFHeightPickerView.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/17.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFHeightPickerView.h"
#import "YFUserSeriesViewModel.h"

@interface YFHeightPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UILabel *titlelabel;
@property (weak, nonatomic) IBOutlet UIButton *confirmButton;

/** 第一列默认显示的文字 */
@property (nonatomic, copy)NSString *firstColValue;
/** 第二列默认显示的文字 */
@property (nonatomic, copy)NSString *secondColValue;

@property (nonatomic, strong) NSArray *inchArrays;
//
@property (nonatomic, strong) NSArray *ftArrays;
/** 公制的数据源 */
@property (nonatomic, strong) NSArray *cmArrays;

@property (nonatomic, copy)NSString *currentFtStr;

@property (nonatomic, copy)NSString *currentInchStr;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *centerCon;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *centerRightCon;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightLineWIdthCon;

@end

@implementation YFHeightPickerView

- (void)awakeFromNib {
    [super awakeFromNib];
    if ([YFUserDefaults generalMetricSystemUnit]) {
        _centerRightCon.priority = 333;
        _centerCon.priority = 999;
        _rightLineWIdthCon.constant = 0;
    }else {
        _centerRightCon.priority = 999;
        _centerCon.priority = 333;
        _rightLineWIdthCon.constant = 60;
    }
    [self clipsCornerRadius:6 borderWidth:1.5 borderColor:kYellowGreenColor];
    [self.confirmButton clipsCornerRadius:3];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
}

- (IBAction)confirmButtonClick:(id)sender {
    if (self.didSelectHeight) {
        self.didSelectHeight(self.currentFtStr,self.currentInchStr);
    }
}

- (NSInteger)numberOfComponentsInPickerView:(nonnull UIPickerView *)pickerView {
    return [YFUserDefaults generalMetricSystemUnit] ? 1:2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if ([YFUserDefaults generalMetricSystemUnit]) {
        return self.cmArrays.count;
    }else {
        if (component == 0) {
            return self.ftArrays.count;
        }else {
            return self.inchArrays.count;
        }
    }
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    
    ((UIView *)[pickerView.subviews objectAtIndex:1]).backgroundColor = [UIColor clearColor];
    UILabel *pickerLabel = (UILabel *)view;
    if (!pickerLabel) {
        pickerLabel = [[UILabel alloc] init];
        UIFont *font = kOpenSansFont(OpenSansExtraBold, 20);
        pickerLabel.font = font;
        pickerLabel.textColor = kTWhiteText;
    }
    NSString *title = @"";
    if ([YFUserDefaults generalMetricSystemUnit] == NO) {
        ((UIView *)[pickerView.subviews objectAtIndex:2]).backgroundColor = [UIColor clearColor];
        if (component == 0) {
            title = self.ftArrays[row];
        }else{
            title = self.inchArrays[row];
        }
    }else {
        title = self.cmArrays[row];
    }
    
    pickerLabel.text = title;
    [pickerLabel sizeToFit];
    return pickerLabel;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if ([YFUserDefaults generalMetricSystemUnit]) {
        self.currentFtStr = self.cmArrays[row];
    }else {
        if (component == 0) {
            self.currentFtStr = self.ftArrays[row];
        }else {
            self.currentInchStr = self.inchArrays[row];
        }
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 48;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return kAutoWid(106);
}

+ (instancetype)ftInchPickerView {
    YFHeightPickerView *view = [[NSBundle mainBundle] loadNibNamed:@"YFHeightPickerView" owner:nil options:nil].lastObject;
    return view;
}

- (void)configuerFirstColValue:(NSString *)firstColValue secondColValue:(NSString *)secondColValue {
    self.firstColValue = firstColValue;
    self.secondColValue = secondColValue;
    self.currentFtStr = firstColValue;
    self.currentInchStr = secondColValue;
    if ([YFUserDefaults generalMetricSystemUnit]) {
        if ([self.cmArrays containsObject:self.firstColValue]) {
            NSInteger oneIndex = [self.cmArrays indexOfObject:self.firstColValue];
            [self.pickerView selectRow:oneIndex inComponent:0 animated:NO];
        }
    }else {
        if ([self.ftArrays containsObject:self.firstColValue]) {
            NSInteger oneIndex = [self.ftArrays indexOfObject:self.firstColValue];
            [self.pickerView selectRow:oneIndex inComponent:0 animated:NO];
        }
        if ([self.inchArrays containsObject:self.secondColValue]) {
            NSInteger twoIndex = [self.inchArrays indexOfObject:self.secondColValue];
            [self.pickerView selectRow:twoIndex inComponent:1 animated:NO];
        }
    }
}
- (NSArray *)inchArrays {
    if (_inchArrays == nil) {
        NSMutableArray *inchArrM = [NSMutableArray array];
        for (NSInteger i = 0; i < 12; i++) {
            [inchArrM addObject:@(i).stringValue];
        }
        _inchArrays = inchArrM.copy;
    }
    return _inchArrays;
}

- (NSArray *)ftArrays {
    if (_ftArrays == nil) {
        NSMutableArray *ftArrM = [NSMutableArray array];
        for (NSInteger i = 1; i <= 8; i++) {
            [ftArrM addObject:@(i).stringValue];
        }
        _ftArrays = ftArrM.copy;
    }
    return _ftArrays;
}
- (NSArray *)cmArrays {
    if (_cmArrays == nil) {
        _cmArrays = [YFUserSeriesViewModel shareInstance].heights;
    }
    return _cmArrays;
}

@end
